package com.kazimsoylu.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.script.SimpleScriptContext;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import net.sf.jml.MsnContact;
import net.sf.jml.MsnSwitchboard;
import net.sf.jml.impl.BasicSwitchboard;
import net.sf.jml.impl.SimpleSwitchboard;

import com.kazimsoylu.Application;
import com.kazimsoylu.ContactListRenderer;
import com.kazimsoylu.MessengerFunctions;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Messenger extends JFrame
{
	private static final long serialVersionUID = 7811630874413906724L;
	
	private static Messenger instance = null;
	
	private JPanel contentPane;
	private JList contactList;
	private DefaultListModel contactListModel;
	
	public static Messenger getInstance()
	{
		synchronized (Messenger.class) 
		{
			if( instance == null )
				instance = new Messenger();
		}
		
		return instance;
	}

	private Messenger() 
	{
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 300, 480);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnNewMenu = new JMenu("Dosya");
		menuBar.add(mnNewMenu);
		

		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		contactListModel = new DefaultListModel();
		contactList = new JList();
		contactList.setCellRenderer( new ContactListRenderer() );
		contactList.addMouseListener(new MouseAdapter() 
		{
			@Override
			public void mouseClicked(MouseEvent e) 
			{
				MsnContact contact = (MsnContact)contactList.getSelectedValue();
				ConversationWindow cw = Application.getConversation( contact );
				if( cw == null )
				{			
					cw = new ConversationWindow( contact );
					Application.addConversation(contact, cw);
					cw.setVisible(true);
					
					
					//Application.addSwitchBoard( contact , switchboard);
				}
				
			}
		});
		contactList.setBounds(10, 11, 268, 415);
		contactList.setModel( contactListModel );
		contentPane.add(contactList);
	}
	
	public void fillContactList( MsnContact[] contacts )
	{
		for( MsnContact c : contacts )
			contactListModel.addElement( c );
	}
	
	
}
